﻿using System;
using System.Windows.Forms;
using ComputerVision.Common;
using ComputerVision.Forms;
using ComputerVision.LoaiXeForm;
using ComputerVision.Role;
using ComputerVision.User;
using ComputerVision.VeXeForm;
using System.Web.Security;


namespace ComputerVision
{
    public partial class frmMain : Form
    {

        public frmMain()
        {
            InitializeComponent();
            ShowHideMenu();
        }

        #region Closed

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form openForm in Application.OpenForms)
            {
                openForm.Close();
            }
            Application.Exit();
        }

        #endregion

        private void mnuInfo_Click(object sender, EventArgs e)
        {
            var frm = new frmInformation();
            frm.ShowDialog();
        }

        private void CloseMainForm(object sender, FormClosedEventArgs e)
        {
            while (Application.OpenForms.Count > 0)
            {
                Application.OpenForms[0].Close();
            }
        }

        private void mnuItemLogin_Click(object sender, EventArgs e)
        {
            var frm = new frmLogin();
            frm.Show();
            Hide();
        }

        private void mnuItemChangePassword_Click(object sender, EventArgs e)
        {
            var frm = new frmChangePassword();
            frm.ShowDialog();
        }

        private void mnuItemRoles_Click(object sender, EventArgs e)
        {
            var frm = new frmRoleList();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuItemUserInRole_Click(object sender, EventArgs e)
        {
            var frm = new frmUserInRole();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuItemInitCamera_Click(object sender, EventArgs e)
        {
            var frm = new frmConfigCamera();
            frm.ShowDialog();
        }

        private void mnuItemListUser_Click(object sender, EventArgs e)
        {
            var frm = new frmUserList();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuLoaiXe_Click(object sender, EventArgs e)
        {
            var frm = new frmLoaiXeList();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void mnuItemVeXe_Click(object sender, EventArgs e)
        {
            var frm = new frmVeXeList();
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }

        private void trainningCharacterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //var frm = new frmTrainningCharacter();
            //frm.MdiParent = this;
            //frm.WindowState = FormWindowState.Maximized;
            //frm.Show();
        }

        private void menuXeRa_Click(object sender, EventArgs e)
        {
            var frm = new frmXeRa();

            if (Utilities.IsFormOpen(frm))
            {
                Utilities.ActiveForm(frm);
            }
            else
            {
                frm.MdiParent = this;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
        }

        private void menuXeVao_Click(object sender, EventArgs e)
        {
            var frm = new frmXeVao();
            if (Utilities.IsFormOpen(frm))
            {
                Utilities.ActiveForm(frm);
            }
            else
            {
                frm.MdiParent = this;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }

        }

        private void ShowHideMenu()
        {
            //bool user = Roles.IsUserInRole(@"Người dùng");
            //bool admin = Roles.IsUserInRole(@"Quản trị");
            string[] roles = Roles.GetRolesForUser(Constants.CurrentUser);
            if (roles.Length >= 1)
            {
                bool isQuanTri = false;
                foreach (string item in roles)
                {
                    if (item.Trim().ToLower().Equals(Constants.Roles.QuanTri))
                    {
                        isQuanTri = true;
                    }
                }

                if (!isQuanTri)
                {
                    mnuItemRoles.Visible = false;
                    mnuItemUserInRole.Visible = false;
                    mnuItemListUser.Visible = false;
                    //mnuSuCo.Visible = false;

                    editMenu.Visible = false;
                    toolsMenu.Visible = false;
                    trainningToolStripMenuItem.Visible = false;
                }
            }
            else
            {
                mnuItemRoles.Visible = false;
                mnuItemUserInRole.Visible = false;
                mnuItemListUser.Visible = false;
                mnuSuCo.Visible = false;

                editMenu.Visible = false;
                toolsMenu.Visible = false;
                trainningToolStripMenuItem.Visible = false;
                mnuItemInitCamera.Visible = false;
                menuXeVao.Visible = false;
                menuXeRa.Visible = false;

            }
        }

        private void mnuSuCo_Click(object sender, EventArgs e)
        {
            var frm = new frmSuCoVeXe();
            if (Utilities.IsFormOpen(frm))
            {
                Utilities.ActiveForm(frm);
            }
            else
            {
                frm.MdiParent = this;
                frm.WindowState = FormWindowState.Maximized;
                frm.Show();
            }
        }
    }
}